var transfer_info_ar = [], countDowns = [];
document.onmousemove = function(evt) {
	if (!evt) evt = window.event;
	mousex = evt.clientX;
	mousey = evt.clientY;
}

function toolTipBox(x) {
	create_tip();
	showElement('tip');
	$('tip_content').innerHTML = x;
}

function clearToolTipBox() {
	if ($('tip')) {
		hideElement('tip');
	}
}

function create_tip() {
	if (!$('tip')) {
		var newDiv = document.createElement("div");
		newDiv.id = "tip";
		newDiv.className = "tip";
		newDiv.style.visibility = 'hidden';
		newDiv.style.display = 'none';
		newDiv.style.zIndex = '255';
		newDiv.style.width = 'auto';
		tip_content = '<div class="tt_content" id="tip_content"></div>';
		newDiv.innerHTML = tip_content;
		document.body.appendChild(newDiv);
	}
	if ($('tip')) {
		var left = (mousex + 15);
		var top = (mousey + 7);
		$('tip').style.left = left + 'px';
		$('tip').style.top = top + 'px';
	}
}

function echo(str) {
	var date = new Date();
	date = date.getTime();
	date = date.toString();
	$e("dump").innerHTML += date.substring(6, 10)+":"+str+"<br/>";
}


/* Filtering and Sorting */
if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}
color_ret_ar = {"gk":"286A00","d":"0076C5","dm":"FFCC00","m":"FFCC00","om":"FFCC00","f":"CC0000"};
function colorPos(str, str2) {
	var side = str.substr(str.length-1,1);
	var pos = str.substr(0,str.length-1);
	if (str2) {
		var side2 = str2.substr(str2.length-1,1);
		var pos2 = str2.substr(0,str2.length-1);
	}
	if (pos == pos2) {
		pos2 = false;
	}
	var ret = "";
	if (pos == "f" && !pos2) {
		ret = "<span class='f'>"+global_content["f_xxx"]+"</span>";
	} else if (pos == "g") {
		ret = "<span class='gk'>"+global_content["gk_xxx"]+"</span>";
	} else {
		if (pos == "d") {
			ret+="<span class='d'>"+global_content["d_xxx"]+"</span>";
		}
		if (pos == "dm") {
			ret+="<span class='m'>"+global_content["dm_xxx"]+"</span>";
		}
		if (pos == "m") {
			ret+="<span class='m'>"+global_content["m_xxx"]+"</span>";
		}
		if (pos == "om") {
			ret+="<span class='m'>"+global_content["om_xxx"]+"</span>";
		}
		if (pos2 && pos2 != "f") {
			if (pos != "f") {
				ret+="/";
			}
			if (pos2 == "d") {
				ret+="<span class='d'>"+global_content["d_xxx"]+"</span>";
			}
			if (pos2 == "dm") {
				ret+="<span class='m'>"+global_content["dm_xxx"]+"</span>";
			}
			if (pos2 == "m") {
				ret+="<span class='m'>"+global_content["m_xxx"]+"</span>";
			}
			if (pos2 == "om") {
				ret+="<span class='m'>"+global_content["om_xxx"]+"</span>";
			}
		}
		// Side
		ret+=" ";
		if (side == "l" || side2 == "l") {
			ret+=global_content["left_abbr"];
		}
		if ((side == "c" && pos != "f") || (side2 == "c" && pos2 != "f")) {
			ret+=global_content["center_abbr"];
		}
		if (side == "r" || side2 == "r") {
			ret+=global_content["right_abbr"];;
		}
		// Forward
		if (pos == "f"|| pos2 == "f") {
			ret+=", <span class='f'>"+global_content["f_xxx"]+"</span>";
		}
	}
	return ret;
}
function retStars(no) {
	var ret = no;
	if (no == 20) {
		ret = "<img src='/pics/star.png'>";
	} else if (no == 19) {
		ret = "<img src='/pics/star_silver.png'>";
	} else if (no == 0) {
		ret = "<span style='color:#B8D988'>-</span>";
	} else if (no <6) {
		ret = "<span style='color:#B8D988'>"+no+"</span>";
	}
	return ret;
}
function retPosSort(str) {
	var tmp = str.substr(0, 1);
	var tmp2 = str.substr(1, 1);
	var ret;
	if (tmp == "D" && tmp2 != "M") {
		ret = 1;
	} else if (tmp == "M") {
		ret = 2;
	} else if (tmp == "O") {
		ret = 3;
	} else if (tmp == "F") {
		ret = 4;
	} else {
		ret = 0;
	}
	return ret;
}
var sorting = "time";
var asc_ar = ["time","bid","age"]
function sortFunc(a, b) {
	if (sorting == "fp") {
		a = retPosSort(a["fp"][0]);
		b = retPosSort(b["fp"][0]);
	} else {
		a = a[sorting];
		b = b[sorting];
	}
	if(isNaN(a)) {
		return a>b;
	} else if (asc_ar.indexOf(sorting) >= 0) {
		return a-b;
	} else {
		return b-a;
	}
}
var showPerPage = 100;
var pageStart = 0;
function setPages(no) {
	if (no == 25 || no == 50 || no == 100) {
		showPerPage = no;
		$e("pages_25").style.color = "#fff";
		$e("pages_50").style.color = "#fff";
		$e("pages_100").style.color = "#fff";
		$e("pages_"+no).style.color = "#ccff00";
		makeTable(transfer_info_ar);
	}
}
/*
function popPages() {
	var tmp_pages = Math.ceil(transfer_info_ar.length/showPerPage);
	var tmp = "<span onclick='changePage(\"--\");'>&laquo;</span>";
	for (var i = 0; i<tmp_pages; i++) {
		var click_log = "";
//		if(i == 1) click_log="click_action_log(26)";
//		if(i == 2) click_log="click_action_log(27)";
//		if(i == 3) click_log="click_action_log(28)";
		if (i*showPerPage <= pageStart && i*showPerPage+showPerPage > pageStart) {
			tmp += "<span style='color:#ccff00;' onclick='changePage("+i*showPerPage+");"+click_log+"'>"+(i+1)+"</span>";
		} else {
			tmp += "<span onclick='changePage("+i*showPerPage+");"+click_log+"'>"+(i+1)+"</span>";
		}
	}
	tmp += "<span onclick='changePage(\"++\");'>&raquo;</span>";
	$e("pages").innerHTML = tmp;
}
*/
function changePage(n) {
	if (n == "++") {
		pageStart = Math.min(transfer_info_ar.length-showPerPage, pageStart+showPerPage);
	} else if (n == "--") {
		pageStart = Math.max(0, pageStart-showPerPage);
	} else if (n >= 0 || n <= transfer_info_ar.length) {
		pageStart = n;
	}
	makeTable(transfer_info_ar);
}
var breakdown = true;
function filterBreakdown(boo) {
	if (boo) {
		breakdown = true;
		$("#breakdown").css("background-image","url(/pics/sort_btn_selected.gif)");
		$("#skills").css("background-image","url(/pics/sort_btn_selected_off.gif)");
	} else {
		breakdown = false;
		$("#breakdown").css("background-image","url(/pics/sort_btn_selected_off.gif)");
		$("#skills").css("background-image","url(/pics/sort_btn_selected.gif)");
	}
	makeTable(transfer_info_ar);
}
function filter_skills(type, val) {
	var tmp_type = (type == "s") ? "v" : "s";
	if ($("#filter_"+tmp_type+""+val).val() != "0") {
		var tmp = "#"+retHashFromFilters();
		location.hash = tmp;
		if($("#filter_s"+(val+1)).length == 0)
		{
		//	filter_skills_add(val); // done in filtersfromhash
		
		}
	}
}
function filter_skills_add(val)
{
	var $v = $("#filter_v"+val);
	var $v2 = $v.clone();
	var $s2 = $("#filter_s"+val).clone();
	$("#skill_filters").append($s2,$("<span />").text(" "),$v2,$("<br />"));
	$s2.attr("id","filter_s"+(val+1)).attr("onchange","filter_skills('s',"+(val+1)+")").selectmenu({
		"style":"dropdown",
		"maxHeight":"250",
		"width": "100"
		});
	$v2.attr("id","filter_v"+(val+1)).attr("onchange","filter_skills('v',"+(val+1)+")").selectmenu({
		"style":"dropdown",
		"maxHeight":"250",
		"width": "80"
		});
	number_skill_filters++;
}
function filter_tr(x, y) {
	if (x == "gk") {
		filters_ar["de"] = filters_ar["dm"] = filters_ar["mf"] = filters_ar["om"] = filters_ar["fw"] = filters_ar["le"] = filters_ar["ce"] = filters_ar["ri"] = false;
	} else if (x == "de" || x == "dm" || x == "mf" || x == "om" || x == "fw") {
		filters_ar["gk"] = false;
	}
	if (y) {
		filters_ar[x] = y;
	} else {
		filters_ar[x] = (filters_ar[x] == true) ? false : true;
	}
	var tmp = "#"+retHashFromFilters();
	location.hash = tmp;
}
var rev = false;
function sort_it(foo) {
	if (foo) {
		if (foo == sorting) {
			rev = !rev;
		} else {
			sorting=foo;
			rev = false;
		}
	}
	if(transfer_info_ar) {
		transfer_info_ar.sort(sortFunc);
		if (rev) {
			transfer_info_ar.reverse();
		}
		makeTable(transfer_info_ar);
	} else {
		makeTable();
	}
}
var gk_mode = false;
function construct_th(mode) {
	var myRow = myTable.insertRow(-1);
	function make_single_th(assoc) {
		var th = document.createElement('th');
		if (assoc) {
			th.i = assoc;
			th.onclick = function() { sort_it(this.i); };
			if (headers_ar[assoc]["style"]) {
				th.className = headers_ar[assoc]["style"];
			}
			if (headers_ar[assoc]["width"]) {
				th.style.width = headers_ar[assoc]["width"];
			}
			th.innerHTML = headers_ar[assoc]["header"];
			th.title = headers_ar[assoc]["title"];
			th.style.cursor = "pointer";
			myRow.appendChild(th);
		} else {
			th.innerHTML = "-";
			myRow.appendChild(th);
		}
		$(myRow).addClass("header");
	}

	if (breakdown == true) {
		for (var i=0; i<headers_std.length; i++) {
			make_single_th(headers_std[i]);
		}
	} else {
		gk_mode = false;
		if (filters_ar["gk"] == true && filters_ar["de"] == false && filters_ar["dm"] == false && filters_ar["mf"] == false && filters_ar["om"] == false && filters_ar["fw"] == false) {
			gk_mode = true;
		}
		var tmp_ar = ["han","one","ref","ari","jum","com","kic","thr"];
		for (var i in tmp_ar) {
			if (filters_ar[tmp_ar[i]]) {
				gk_mode = true;
			}
		}
		if (gk_mode) {
			for (var i=0; i<headers_skills_gk.length; i++) {
				make_single_th(headers_skills_gk[i]);
			}
		} else {
			for (var i=0; i<headers_skills.length; i++) {
				make_single_th(headers_skills[i]);
			}
		}
	}
}
function retRec(a){
	return " * "+a;
}
function addCommas(a){
	return a;
}
function timeLeft(a)
{
	return a;
}
function popInfo(id) {
	var tmp = false;
	for (var i = 0; i<transfer_info_ar.length; i++) {
		if (transfer_info_ar[i]['id'] == id) {
			tmp = transfer_info_ar[i];
			break;
		}
	}
	if (tmp) {
		var miniTable = document.createElement('table');
		miniTable.cellSpacing = "0";
		miniTable.cellPadding = "0";
		miniTable.style.marginTop = "8px";
		miniTable.style.width = "350px";
		var miniRow = miniTable.insertRow(-1);
		if (breakdown==true) {
			tmp_headers = (tmp["fp"][0] == "gk") ? headers_mini_skills_gk : headers_mini_skills;
		} else {
			tmp_headers = headers_mini_std;
			miniTable.className = "miniTable";
		}
		for (var i=0; i<tmp_headers.length; i++) {
			var miniCell = miniRow.insertCell(-1);
			miniCell.style.padding = "0 2px";
			miniCell.innerHTML = headers_ar[tmp_headers[i]]["header"];
		}
		miniRow = miniTable.insertRow(-1);
		for (var i=0; i<tmp_headers.length; i++) {
			var miniCell = miniRow.insertCell(-1);
			if (tmp_headers[i] == "rec") {
				miniCell.innerHTML = tmp[tmp_headers[i]];
			} else if (tmp_headers[i] == "asi" || tmp_headers[i] == "bid") {
				miniCell.innerHTML = tmp[tmp_headers[i]];
			} else if (tmp_headers[i] == "time") {
				if (tmp[tmp_headers[i]] > 119) {
					miniCell.innerHTML = timeLeft(tmp[tmp_headers[i]])+"<br>"+timeLeftDate(tmp[tmp_headers[i]]);
				} else {
					miniCell.innerHTML = timeLeft(tmp[tmp_headers[i]]);
				}
			} else {
				miniCell.innerHTML = retStars(tmp[tmp_headers[i]]);
			}
		}
		var tmp_str = 0;
		for (var i in tmp) {
			tmp_str += i+": "+tmp[i]+" ";
		}
/*
		create_tip();
		showElement('tip');
		var basic_text = "<span style='font-weight:bold'>"+pagecontent_34+"</span> "+tmp["name"]+"<br>";
		var age1 = Math.floor(tmp["age"]);
		var age2 = Math.round((tmp["age"]-age1)*100);
		basic_text += "<span style='font-weight:bold'>"+pagecontent_35+"</span> "+age1+"yr "+age2+"mo"+"<br>";
		basic_text += "<span style='font-weight:bold'>"+pagecontent_36+"</span> "+tmp["club_name"];
		$('tip_content').innerHTML = basic_text;
		$('tip').style.width = 'auto';
		$('tip_content').appendChild(miniTable); */
		//toolTipBox(tmp_str);
	}
}

function construct_tr(ply_ar, count, mode) {
	var myRow = myTable.insertRow(-1);
	$(myRow).attr("id", "player_row_"+ply_ar['id']);

// now they are put in the top and made greener
//	if(ply_ar["user_is_buyer"]) $(myRow).addClass("highlighted_row");

	if (ply_ar["pimp"] >= 1) {
		$(myRow).addClass("pimp_bold");
	}
	if (ply_ar["pimp"] == 3) {
		$(myRow).addClass("pimp_color");
	}
	
	if (ply_ar.bump) {
		var $myRow = $(myRow).insertAfter($(myTable).find('tr').first()).addClass('watched-player');
		if (ply_ar.bidder_id == SESSION.id) {
			$myRow.addClass('watched-player-currentbid');
		} else if (ply_ar.bump_shortlist) {
			$myRow.addClass('watched-player-shortlist');
		} else if (ply_ar.bidder_id > 0) {
			$myRow.addClass('watched-player-outbid');
		}
	}
	function construct_cell(assoc) {
		if( ply_ar.time < 2 ) return;
		if(!show_skill_index && assoc == "asi") return;
		var myCell = myRow.insertCell(-1);
		if (headers_ar[assoc]["style"]) {
			myCell.className = headers_ar[assoc]["style"];
		}
		if (assoc == "-" || assoc == "border") {
			myCell.innerHTML = "-";
		} else if (assoc == "nat") {
			if (ply_ar["nat"]) {
				myCell.innerHTML = ply_ar["flag"];
				$(myCell).addClass("align_center");
			}
		} else if (assoc == "name") {
			myCell.noWrap = true;
			var img;
			if (ply_ar["txt"]) {
				img = $("<img src=\"/pics/icons/transfer_speechbubble.gif\" />");
				img.tooltip(ply_ar["txt"],"text");
			}

			$("<div />").addClass("player_name").html("").append(img).append(ply_ar["player_link"]).append(" ").appendTo(myCell);
		} else if (assoc == "rec") {
			myCell.innerHTML = "<span sort=\""+ply_ar["rec"]+"\">"+rec_format(ply_ar["rec"])+"</span>";
			$(myCell).addClass("align_center");
		} else if (assoc == "asi" || assoc == "bid") {
			myCell.innerHTML = "<span sort=\""+ply_ar[assoc]+"\">"+ply_ar[assoc+"_string"]+"</span>";
			if (assoc=="bid") $(myCell).addClass("bid_"+ply_ar["id"]);
			$(myCell).addClass("align_right");
		} else if (assoc == "bid_btn") {
			$(myCell).html(make_button("<img src='/pics/auction_hammer_small.png' class='auction_hammer' style='position:relative;top: 2px;'>","tlpop_pop_transfer_bid('"+ply_ar["next_bid"]+"',"+ply_ar["pro"]+",'"+ply_ar["id"]+"','"+ply_ar["name_js"]+"');click_action_log(31,SESSION['tutorial']>0);","small_button"));
			$(myCell).addClass("align_center bid_button");

		} else if (assoc == "scout_btn") {
			if(ply_ar["shortlisted"] == 0)
			{
				$(myCell).html(make_button("<img src='/pics/icons/clips.gif' style='position:relative;top: 2px;'>","short_list_player("+ply_ar["id"]+",true);button_disable($(this));click_action_log(34,SESSION['tutorial']>0);","small_button"));
			}
			else
			{
				$(myCell).html(make_button("<img src='/pics/icons/clips.gif' style='position:relative;top: 2px;'>","","small_button disabled"));
			}
				$(myCell).addClass("align_center");
		} else if (assoc == "age") {
			myCell.innerHTML = Math.floor(ply_ar["age"]);
			$(myCell).addClass("align_center");
		} else if (assoc == "time") {
			myCell.innerHTML = ply_ar["time"];
			$(myCell).attr("id", "time_"+ply_ar["id"]);
			$(myCell).addClass("time-left-td align_center");
			
			var cd = new Countdown(ply_ar["time"], '', "highest", true, function(cntdwn) {
				if (!cntdwn || !cntdwn.getJQ) return;
				var $tr = cntdwn.getJQ().closest('tr');
				if ($tr.hasClass('watched-player-currentbid')) {
					$tr.find('.time-left-td').html(pagecontent['t75']).addClass('bold align_center').removeClass('time-left-td');
				} else if ($tr.hasClass('watched-player-outbid')) {
					$tr.find('.time-left-td').html(pagecontent['t76']).addClass('bold align_center').removeClass('time-left-td');
				} else {
					$tr.fadeOut(150000, function() {
						$tr.remove();
						});
					$tr.find('td').css({color: '#ff3636'});
					$tr.find('.auction_hammer').remove();
					cntdwn.getJQ().closest('td').html('-');
				}
			/*	$tr.fadeOut(1500, function() {
					$tr.remove();
					});
				$tr.find('td').css({color: '#ff3636'});
				cntdwn.getJQ().closest('td').html('-');
				*/
			});

			countDowns[ply_ar['id']] = cd;
			$(myCell).html(countDowns[ply_ar['id']].getJQ());
		} else if (assoc == "fp") {
			myCell.innerHTML = "<span class='favposition short nowrap' style='vertical-align:middle'>"+colorPos(ply_ar["fp"][0], ply_ar["fp"][1])+"</span>";
			$(myCell).addClass("align_center");
		} else if (search_skills_ar.indexOf(assoc) >= 0) {
			myCell.innerHTML = retStars(ply_ar[assoc]);
		} else {
			myCell.innerHTML = ply_ar[assoc];
		}
	}

	var tmp_ar;
	if (breakdown == true) {
		tmp_ar = headers_std;
	} else {
		if (gk_mode) {
			tmp_ar = headers_skills_gk;
		} else if (ply_ar["fp"][0] == "gk") {
			tmp_ar = headers_skills_hybrid;
		} else {
			tmp_ar = headers_skills;
		}
	}
	for (var i=0; i<tmp_ar.length; i++) {
		construct_cell(tmp_ar[i]);
	}
}
var myTable = null;
function makeTable(arr) {
	//echo("<span style='color:#f00'>making table</span>");
	//popPages();
	if (arr) {
		if(is_tutorial) var arr = arr.slice(0,5);
		myTable = document.createElement('table');
		$(myTable).addClass("hover");
		myTable.cellSpacing = "0";
		myTable.cellPadding = "0";
		construct_th();
		for (var i=pageStart; i<Math.min(pageStart+showPerPage, arr.length); i++) {
			construct_tr(arr[i], i);
		}
		$("#transfer_list").html("");
		table_zebra($(myTable));
		activate_player_links($(myTable).find("[player_link]"));
		init_tooltip_by_elems($(myTable).find("[tooltip]"));
		$("#transfer_list").append(myTable);
		make_highlighted_rows();

		$("#hits").html((arr.length>=200) ? ">200" : arr.length);
		if(is_tutorial)
		{
			$(myTable).append(
				$("<tr>").append(
					$("<td>").addClass("align_center").attr("colspan",100).html($("<span />").addClass("faux_link bold").html(pagecontent[46]).click(function(){
						is_tutorial=false;
						sort_it();
					}))
				)
			);
//			setTimeout(function(){add_arrow_to_element($(".bid_button:first"));},1000);
		}
		if(arr.length == 0)
		{
			var myRow = myTable.insertRow(-1);
			$(myRow).append($("<td>").attr("colspan",100).addClass("align_center bold").html(pagecontent[39]).append("<br />"+pagecontent[40]));
		}
	}
}

function timeLeftDate(s) {
	var ex_date = new Date();
	ex_date.setSeconds(ex_date.getSeconds() + s);
	return ex_date.format("yyyy/mm/dd - HH:MM");
}
var search_true_ar = ["gk","de","dm","mf","om","fw","le","ce","ri","for"];
var search_skills_ar = ["str","sta","pac","mar","tac","wor","pos","pas","cro","tec","hea","fin","lon","set","han","one","ref","ari","jum","com","kic","thr"];
if(transfer_settings["advanced"]) var search_bump_ar = ["time","amin","amax","rmin","rmax","cost"];
else var search_bump_ar = ["time","amax","rmin","cost"];

function searchTrigger() {
	//echo("search triggered");
	var tmp = "#"+retHashFromFilters();
	if (location.hash == tmp) {
		startSearch();
	} else {
		location.hash = tmp;
	}
}

function retHashFromFilters() {
	//echo("ret hash from filters");
	var ret = "/";
	// Pos
	for (var i=0; i<search_true_ar.length; i++) {
		if (filters_ar[search_true_ar[i]] == true) {
			ret += search_true_ar[i]+"/"
		}
	}
	// Recommendation, Age and Time
	if(transfer_settings["advanced"]) tmp_ar = [0,18,40,0,10,0];
	else tmp_ar = [0,40,0,0];
	for (var i=0; i<search_bump_ar.length; i++) {
		tmp = $("#filter_"+search_bump_ar[i]).val();
		if (tmp != tmp_ar[i]) {
			ret += search_bump_ar[i]+"/"+tmp+"/";
		}
		filters_ar[search_bump_ar[i]] = tmp;
	}
	// Skills
	for (var i=0; i<number_skill_filters; i++) {
		tmp = $("#filter_s"+i).val();
		tmp2 = $("#filter_v"+i).val();
		if (tmp != 0 && tmp2 != 0) {
			ret += tmp+"/"+tmp2+"/";
		}
	}
	return ret;

}

function setFiltersFromHash(tmp_hash) {
	//echo("set filters from hash");
	filters_ar = {
		"gk":false,"de":false,"dm":false,"mf":false,"om":false,"fw":false,
		"le":false,"ce":false,"ri":false,
		"amin":18,"amax":40,
		"rmin":0,"rmax":10,
		"for":true,
		"cost":'aff',
//		"cost":0,
		"time":0
	}
	if(transfer_settings["advanced"]) filters_ar["cost"] = 0;
	//echo("Hash: "+location.hash);
	tmp_hash = tmp_hash || location.hash;
	location.hash=tmp_hash;
	if(tmp_hash) {
		filters_ar["for"] = false;
		tmp_hash = tmp_hash.substr(1);
		tmp_hash = tmp_hash.split("/");
		for(var i=0; i<tmp_hash.length; i++) {
			if(search_true_ar.indexOf(tmp_hash[i]) >= 0) {
				filters_ar[tmp_hash[i]] = true;
			} else if (search_bump_ar.indexOf(tmp_hash[i]) >= 0 || search_skills_ar.indexOf(tmp_hash[i]) >= 0) {
				filters_ar[tmp_hash[i]] = tmp_hash[i+1];
			}
		}
	}
	setTimeout("transfer_style_selects()",200);
}

function popFilterImages() {
	//echo("pop filter images");
	// Pos
	if (filters_ar["gk"] == true) {
		$("#filter_gk").css("background-image","url(/pics/sort_btn_g_on.gif)");
	} else {
		$("#filter_gk").css("background-image","url(/pics/sort_btn_g_off.gif)");
	}
	if (filters_ar["de"] == true) {
		$("#filter_de").css("background-image","url(/pics/sort_btn_d_on.gif)");
	} else {
		$("#filter_de").css("background-image","url(/pics/sort_btn_d_off.gif)");
	}
	if (filters_ar["dm"] == true) {
		$("#filter_dm").css("background-image","url(/pics/sort_btn_m_on.gif)");
	} else {
		$("#filter_dm").css("background-image","url(/pics/sort_btn_m_off.gif)");
	}
	if (filters_ar["mf"] == true) {
		$("#filter_mf").css("background-image","url(/pics/sort_btn_m_on.gif)");
	} else {
		$("#filter_mf").css("background-image","url(/pics/sort_btn_m_off.gif)");
	}
	if (filters_ar["om"] == true) {
		$("#filter_om").css("background-image","url(/pics/sort_btn_m_on.gif)");
	} else {
		$("#filter_om").css("background-image","url(/pics/sort_btn_m_off.gif)");
	}
	if (filters_ar["fw"] == true) {
		$("#filter_fw").css("background-image","url(/pics/sort_btn_f_on.gif)");
	} else {
		$("#filter_fw").css("background-image","url(/pics/sort_btn_f_off.gif)");
	}
	// Side
	if (filters_ar["le"] == true) {
		$e("filter_le").src = "/pics/sort_btn_l_on.gif";
	} else {
		$e("filter_le").src = "/pics/sort_btn_l_off.gif";
	}
	if (filters_ar["ce"] == true) {
		$e("filter_ce").src = "/pics/sort_btn_c_on.gif";
	} else {
		$e("filter_ce").src = "/pics/sort_btn_c_off.gif";
	}
	if (filters_ar["ri"] == true) {
		$e("filter_ri").src = "/pics/sort_btn_r_on.gif";
	} else {
		$e("filter_ri").src = "/pics/sort_btn_r_off.gif";
	}
	// Other
	if (filters_ar["for"] == true) {
		$("#filter_for").css("background-image","url(/pics/sort_btn_selected.gif)");
	} else {
		$("#filter_for").css("background-image","url(/pics/sort_btn_selected_off.gif)");
	}
	// Recommendation, Age and Time
	for (var i=0; i<search_bump_ar.length; i++) {
		tmp = filters_ar[search_bump_ar[i]];
		$e("filter_"+search_bump_ar[i]).value = tmp;
	}
	// Skills
	var tmp_f = 0;
	for (var i=0; i<search_skills_ar.length; i++) {
		tmp = filters_ar[search_skills_ar[i]];
		if (tmp) {
			if($("#filter_s"+tmp_f).length == 0) filter_skills_add(tmp_f-1);
			$e("filter_s"+tmp_f).value = search_skills_ar[i];
			$e("filter_v"+tmp_f).value = tmp;
			tmp_f++;
		}
		if (tmp_f>=10) {
			break;
		}
	}
	if(tmp_f > 0 && tmp_f<10) if($("#filter_s"+(tmp_f)).length == 0) filter_skills_add(tmp_f-1);
	while (tmp_f<number_skill_filters) {
		$e("filter_s"+tmp_f).value = 0;
		$e("filter_v"+tmp_f).value = 0;
		tmp_f++;
	}
}

function makeHashFromInput() {
	if(transfer_settings["advanced"]) tmp_ar = [0,18,40,0,5];
	else tmp_ar = [0,18,40,0,5,'aff'];
//	else tmp_ar = [0,18,40,0,5,0];
	tmp_ar = [0,18,40,0,5,0];
	for (var i=0; i<search_bump_ar.length; i++) {
		tmp = $e("filter_"+search_bump_ar[i]).value;
		filters_ar[search_bump_ar[i]] = tmp;
	}
}

// Hash listener
var current_hash = location.hash;
function check_hash() {
	if (location.hash != current_hash ) {
		current_hash = location.hash;
		pageStart = 0;
		setFiltersFromHash();
		popFilterImages();
		startSearch();
	}
}
var is_tutorial;
function startSearch() {
	$e("transfer_list").innerHTML = get_loading_img("big");
	// Call JSON
	tmp_url = retHashFromFilters();
	$.post("/ajax/transfer.ajax.php",{"search":tmp_url,"club_id":SESSION["id"]},function(data){
		if(data != null)
		{
			if(data["refresh"]) 
			{
				page_refresh();
				return;
			}
			transfer_info_ar = data["list"];
			is_tutorial=data["tutorial"];
			if (transfer_info_ar["error"] == "no session") {
				//NO SESSION!
				$("#transfer_list").html("<p class='error big'>You're not logged in, please <a href='index.php'>log in</a> again to use the transferlist.</p>");
			} else {
				sort_it();
			}
		}
		else
		{
			$e("transfer_list").innerHTML = "<span style='padding-top:30px; font-size:15px; font-weight:bold;'><br />Nothing found</span>";
			$e("hits").innerHTML = 0;
		}

	},"json");
}

$(document).ready(function(){
	setFiltersFromHash();
	popFilterImages();
	hashCheck = setInterval("check_hash()", 50);
	startSearch();
	transfer_style_selects();
});
function transfer_style_selects()
{
	$("select#filter_rmin").selectmenu({
		"style":"dropdown",
		"maxHeight":"250",
		"width": "90",
		format: rec_format
	});
	if($("#filter_rmin").is(":disabled")) $("#filter_rmin").selectmenu("disable");

	$("select#filter_rmax").selectmenu({
		"style":"dropdown",
		"maxHeight":"250",
		"width": "90",
		format: rec_format
	});
	if($("#filter_rmax").is(":disabled"))	$("#filter_rmax").selectmenu("disable");

	$("select#filter_time").selectmenu({
		"style":"dropdown",
		"maxHeight":"250",
		"width": "90"
	});
	$("select#filter_cost").selectmenu({
		"style":"dropdown",
		"maxHeight":"250",
		"width": "90"
	});
	$("select#filter_amin").selectmenu({
		"style":"dropdown",
		"maxHeight":"250",
		"width": "50"
	});
	$("select#filter_amax").selectmenu({
		"style":"dropdown",
		"maxHeight":"250",
		"width": "50"
	});
	for(var i = 0;  i < number_skill_filters ;i++ )
	{
		$("#filter_s"+i).selectmenu({
			"style":"dropdown",
			"maxHeight":"250",
			"width": "100"
		});
		if($("#filter_s"+i).is(":disabled")) $("#filter_s"+i).selectmenu("disable");
		$("#filter_v"+i).selectmenu({
			"style":"dropdown",
			"maxHeight":"250",
			"width": "80"
		});
		if($("#filter_v"+i).is(":disabled")) $("#filter_v"+i).selectmenu("disable");
	}
}

function get_bookmarks(add_new)
{
	modal.html(get_loading_img()).show();
	$.post("/ajax/transfer_get.ajax.php",{"type":"get_bookmarks"},function(data){
		var $d =  $("<div />").append($("<h2 />").html(pagecontent[47]));
		$d.append(
			$("<div />").addClass("new_bookmark_search").append(
				$("<hr />"),
				$("<form />").append(
					$("<p />").addClass("bold large").html(pagecontent[49]),
					$("<label />").css({"display":"inline-block","width":"120px","text-align":"right"}).attr("for","new_bookmark_search").html(pagecontent[50]+" "),
					$("<input />").attr("id","new_bookmark_search").val(location.hash).addClass("disabled embossed").attr("disabled",true),
					$("<br />"),
					$("<label />").css({"display":"inline-block","width":"120px","text-align":"right"}).attr("for","new_bookmark_name").html(pagecontent[51]+" "),
					$("<input />").attr("id","new_bookmark_name").attr("placeholder","4* FC, 20 Fin").addClass("embossed").attr("maxlength",30),
					$("<br />"),
					$("<div />").css({"display":"inline-block","width":"120px","text-align":"right"}),
					$("<label />").css({"display":"inline-block","width":"120px","text-align":"right"}).attr("for","new_bookmark_pm").html(pagecontent[52]),
					$("<input />").attr("id","new_bookmark_pm").attr("type","checkbox").val(1),
					$("<span />").html(" <img src='/pics/icons/question_mark_small.png'>").tooltip(pagecontent[53]),
					$("<br />"),
					$("<div />").css({"display":"inline-block","width":"120px","text-align":"right"}),
					$("<label />").attr("for","new_bookmark_email").html(pagecontent[54]),
					$("<input />").attr("id","new_bookmark_email").attr("type","checkbox").val(1),
					$("<span />").html(" <img src='/pics/icons/question_mark_small.png'>").tooltip(pagecontent[55]+"<br /> <br /><span class='italic'>"+pagecontent[56]+"</span>"),
					$("<p>").html(pc_replace(pagecontent[57],{"[count]":5})),
					$("<div />").addClass("msgbuttons").append(
						make_button(global_content.save,"save_bookmark()"),
						$("<span />").addClass("button").html("<span class='button_border'>"+global_content.cancel+"</span>").click(function(){
							$(".new_bookmark_search").hide();
							$(".new_bookmark_search_button").show();
						})
						)
					),
					$("<hr />")
				).hide(),
				$("<div />").addClass("align_right new_bookmark_search_button").html($("<span />").addClass("button").html("<span class='button_border'>"+pagecontent[48]+"</span>").click(function(){
					$(".new_bookmark_search").slideDown();
					$(".new_bookmark_search_button").hide();
					$("#new_bookmark_name").focus();
				}))
			);
		if(data.bookmarks.length)
		{
			$d.append(
				$("<table />").addClass("zebra").append(
					$("<tr />").append(
						$("<th/>").html("Name"),
						$("<th/>").html("PM"),
						$("<th/>").html("Email"),
						$("<th/>").html("Created"),
						$("<th/>").html("")
					)
				)
			);
			for(var i in data.bookmarks)
			{
				var b = data.bookmarks[i];
				$d.find("table").attr("cellspacing",0).attr("cellpadding",0).append(
					$("<tr />").attr("bookmark",b.id).append(
						$("<td/>").html($("<span />").html(b.name).attr("hash",b.search).addClass("faux_link unbold").click(function(e){
								if(e.ctrlKey)
								{
									window.open("/transfer/"+$(this).attr("hash"));
								}
								else
								{
									location.hash=$(this).attr("hash");
									modal.hide();
								}
						})).tooltip("Search: "+b.search+"<br />"+pagecontent[58]),
						$("<td/>").addClass("align_center").html(parseInt(b.send_pm)?'<img src="/pics/mini_green_check.png">':""),
						$("<td/>").addClass("align_center").html(parseInt(b.send_email)?'<img src="/pics/mini_green_check.png">':""),
						$("<td/>").addClass("align_center").html(b.time),
						$("<td/>").html($("<span/>").html("<img src='/pics/small_red_x.png' class='delete' />").attr("bookmark",b.id).tooltip(global_content["delete"]).click(function(){delete_bookmark($(this).attr("bookmark"))}))
					)
				);
			}
		}
		else
		{
			$d.append($("<div />").addClass("bold align_center").html(pagecontent[59]));
		}
		$d.append($("<div />").addClass("align_center").html(make_button(global_content.close,"modal.hide()")));
		modal.html($d).show();
		make_checkbox("new_bookmark_pm");
		make_checkbox("new_bookmark_email");
	},"json").error(function(){modal.html(global_content.error).show()});
}
function save_bookmark(confirm)
{
	modal.inner().find(".msgbuttons").append(get_loading_img()).find(".button").hide();
	var name = $("#new_bookmark_name").val();
	var pms = $("#new_bookmark_pm").is(":checked")?1:0;
	var emails = $("#new_bookmark_email").is(":checked")?1:0;
	$.post("/ajax/transfer_get.ajax.php",{"type":"save_bookmark","search":location.hash,"name":name,"send_pm":pms,"send_email":emails},function(data){
		if(data["success"])	get_bookmarks();
		else
		{
			var $msg = modal.inner().find(".msgbuttons");
			$msg.find("img.loading").remove();
			$msg.find(".button").show();
			$msg.find(".error").remove();
			$msg.prepend($("<p />").addClass("error").html(global_content.error+": "+data.error));
		}
	},"json").error(function(){modal.inner().find(".msgbuttons").html(global_content.error);});
}
function delete_bookmark(bookmark)
{
	modal.inner().find("tr[bookmark="+bookmark+"]").find(".delete").replaceWith(get_loading_img());
	$.post("/ajax/transfer_get.ajax.php",{"type":"delete_bookmark","id":bookmark},function(data){
		modal.inner().find("tr[bookmark="+bookmark+"]").remove();
	});
}
$(document).ready(function(){
	var $d = $("<div />").addClass("align_center").append(
		$("<span/>").addClass("button").append(
			$("<span />").addClass("button_border").append(
			pagecontent[47]+" ",
			"<img src='/pics/pro_icon.png' />"
		)).click(function(){
			get_bookmarks(true);
		})
	).appendTo("#filters");
	if(!has_pro) 
	{
		button_disable($d.find(".button"));
		$d.find(".button").click(function(){window.location="/buy-pro/";});
	}
});
